import { ref } from 'vue'
import MD5 from 'crypto-js/md5'
import { adapter, request } from '@/request'
import { login1ByPost } from '@/core/api/user'
import type { UserInfo } from '@/core/interfaces/user'
import type { RequestWrapper } from '@/core/interfaces/common'

export interface LoginFields {
  username: string
  pwd: string
}

export default function login1() {
  const service = adapter<RequestWrapper<UserInfo>>(request)
  const loading = ref(false)

  const fetch = (data: LoginFields) => {
    const { url, method } = login1ByPost()
    loading.value = true

    return service({
      config: {
        url,
        method,
        data: {
          username: data.username,
          pwd: MD5(data.pwd).toString()
        }
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [null, fetch, loading] as const
}
